/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>		    All right reserved. 		     </p>
 * <p>项目名称 ： 	 深圳公路信息资源整合及国省检日常化监管管理        </p>
 * <p>创建者   :	詹春贤 
 * <p>描   述  :   路网服务-应急抢修  </p>
 * <p>最后修改 : $: 2017年9月1日-上午12:49:49 v 1.0.0	 Administrator   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.lwfw;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.lwfw.EmergRepairCheckDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.lwfw.EmergRepair;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.lwfw.EmergRepairCheckService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.DateUtils;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 
 * EmergRepairCheckServiceImpl
 * 
 * 2017年9月1日 上午12:49:49
 * 
 * @version 1.0.0
 * 
 */

@Service
@Transactional
public class EmergRepairCheckServiceImpl implements EmergRepairCheckService{
	
	
	@Autowired
	private SystemManageService systemManageService;
	@Autowired
	private CheckTableService checkTableService;
	@Autowired
	private EmergRepairCheckDao emergRepairCheckDao;

	@Override
	public Map<String, Object> getEmergRepairChecks(String startTime, String endTime, CheckTable checkTable, String tag) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Attachment> list = new ArrayList<Attachment>();
		List<EmergRepair> emergRepairs = new ArrayList<EmergRepair>();
		List<String> tempLists = null;
		List<List<String>> resLists = new ArrayList<List<String>>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double tableFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		double percentage = 0; //完善率
		double total = 0; //计算总分
		
		//根据时间、标签查询是否有附件
		list = systemManageService.getCheckAttachments(startTime, endTime, tag);
		//根据开始时间和结束时间查询所有的数据文件
		emergRepairs = getCheckData(startTime, endTime, true);
		if (emergRepairs.size() > 0) {
			double reportNum = 0;
			double restoreNum = 0;
			double ratio = 0;
			//得到所有的相关数据附件
			map.put("manageData", emergRepairs);
			//查出符合条件的数据并汇总返回
			emergRepairs = emergRepairCheckDao.calScoreByEmergRepairs(startTime, endTime);
			for (EmergRepair repair : emergRepairs) {
				tempLists = new ArrayList<String>();
				tempLists.add(repair.getYear());
				tempLists.add(repair.getReportNum().toString());
				tempLists.add(repair.getRestoreNum().toString());
				tempLists.add(repair.getRatio().toString());
				resLists.add(tempLists);
				reportNum += repair.getReportNum();
				restoreNum += repair.getRestoreNum();
			}
			ratio = MathsUtils.divDouble(restoreNum * 100, reportNum, 2);
			if (ratio >= 95) {
				total = tableFull;
			} else if(ratio >= 85) {
				total = yearPart;
			} else {
				total = 0;
			}
			//为了避免数据为空报错，需要确定数据非空才添加
			map.put("reportNum", reportNum);
			map.put("restoreNum", restoreNum);
			map.put("ratio", ratio);
			map.put("flag", true);
		}
		percentage = MathsUtils.divDouble(total*100, tableFull, 2);
		
		//只有当系统完善度和检查得分不相等的情况下才更新数据  避免每次搜索都更新数据
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("att", list);
		map.put("resLists", resLists);
		map.put("percent", percentage);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		return map;
	}
	
	/**
	 * getCheckData:
	 * 适用:根据startTime, endTime检索数据文件列表
	 * @param startTime = 项目开始时间
	 * @param endTime = 项目结束时间
	 * @param b = 是否升序
	 * @return 
	 * @exception 
	 * @since  1.0.0
	*/
	@Override
	public List<EmergRepair> getCheckData(String startTime, String endTime, boolean b) {
		QueryFilter queryFilter = new QueryFilter();
		if (StringUtils.isNotEmpty(startTime) && DateUtils.isDate(startTime)) {
			queryFilter.addFilter("year", startTime, MatchType.GE);
		}
		if (StringUtils.isNotEmpty(endTime) && DateUtils.isDate(endTime)) {
			queryFilter.addFilter("year",  endTime, MatchType.LE);
		}
		return emergRepairCheckDao.find(queryFilter, "year", true);
	}
}
